package com.ld.admin.service.usercenter;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.ld.admin.model.usercenter.SysUserNoteModel;
import com.ld.common.util.security.ProjectSecurityUtil;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.core.util.StringUtils;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.PageNavigationBean;
import com.ld.shieldsb.dao.model.QueryModel;

/**
 * 
 * 用户便签/备忘录服务类，@Service注解用于自动注入
 * 
 * @ClassName SysUserNoteService
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年07月02日 11:19:33
 *
 */
@Service
public class SysUserNoteService extends UserCenterBasicService {

    // 用于子类覆盖，扩展doSearch方法中QueryModel的处理（标准queryModel处理完，查询进行前的处理）
    @Override
    protected QueryModel extendDealQuery(QueryModel queryModel) {
        // TODO Auto-generated method stub
        return super.extendDealQuery(queryModel);
    }

    // 用于子类覆盖，扩展doSearch方法中PageNavigationBean的处理（查询完成后进行）
    @Override
    protected <T> PageNavigationBean<T> extendDealPageNavigationBean(PageNavigationBean<T> pageNavigationBean) {
        // TODO Auto-generated method stub
        return super.extendDealPageNavigationBean(pageNavigationBean);
    }

    // 删除方法子类扩展（删除完成后进行）
    @Override
    protected <T> boolean extendRemove(Boolean flag, Class<T> classOfT, String key, Object value, Integer status, Long userId) {
        // TODO Auto-generated method stub
        return flag;
    }

    // AddOrUpdate扩展（AddOrUpdate方法完成后进行）
    @Override
    protected <T> boolean extendAddOrUpdate(Boolean flag, T modelBean, boolean update, String key, Long userId) {
        // TODO Auto-generated method stub
        return flag;
    }

    // 获取翻页信息
    @Override
    public <T> PageNavigationBean<T> getPageBean(HttpServletRequest request, Class<T> classOfT, QueryModel queryModel) {
        int pageNum = getInt("p");
        queryModel.addEq(SysUserNoteModel::getCreateBy, Web.Util.getSessionUserId(request)); //
        return getPageBean(request, classOfT, queryModel, pageNum, PAGE_SIZE);
    }

    // 添加/更新
    @Override
    public <T> Result addOrUpdate(T modelBean, boolean update) {
        SysUserNoteModel model = (SysUserNoteModel) modelBean;
        model.setContent("e:" + ProjectSecurityUtil.simpleWriteEncrypt(model.getContent()));
        if (!update) { // 保存判断个数
            long maxCount = 10L;
            long count = getCount(SysUserNoteModel.class, getValidQueryModel());
            if (count >= maxCount) {
                return Web.Response.error("只能保存" + maxCount + "个便签！");
            }
        }
        return addOrUpdate(modelBean, update);
    }

    /**
     * 获取数据
     * 
     * @Title getData
     * @author 吕凯
     * @date 2019年3月27日 下午2:06:44
     * @param request
     * @param type
     * @return Result
     */
    public Result getData(HttpServletRequest request) {
        QueryModel queryModel = getSearchQueryModel(request);
        List<SysUserNoteModel> list = getAll(SysUserNoteModel.class, queryModel);
        Result result = new Result();
        List<Map<String, Object>> listMap = new ArrayList<>();
        list.stream().forEach(model -> {
            Map<String, Object> dataMap = new LinkedHashMap<>();
            dataMap.put("stickerId", model.getId() + ""); // long精度损失，转为字符型
            String content = model.getContent();
            if (content.startsWith("e:")) {// 加密的,先解密
                content = ProjectSecurityUtil.simpleWriteDecrypt(StringUtils.substringAfter(content, "e:"));
            }
            dataMap.put("content", content); // 内容
            dataMap.put("left", model.getLeftv()); // left
            dataMap.put("top", model.getTopv()); // top
            dataMap.put("width", model.getWidth()); // 宽度
            dataMap.put("height", model.getHeight()); // 高度
            dataMap.put("createTime", model.getCreateTime()); // 高度
            listMap.add(dataMap);
        });
        result.setData(listMap);
        result.setSuccess(true);
        return result;
    }
}
